package com.china08.yxyapi.api.manage.tec.teaching.scoreport.lequestionanalysis;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.scoreport.Exam;
import com.china08.yxyapi.entity.mg.scoreport.report.ExamQuesAnalyse;
import com.china08.yxyapi.entity.mg.scoreport.report.ExamQuesAnalyse.Res;
import com.china08.yxyapi.entity.mg.scoreport.report.ExamQuesOverviewAnalyse;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.mg.scoreport.ExamQuesAnalyseRepsository;
import com.china08.yxyapi.repository.mg.scoreport.ExamQuesOverviewAnalyseRespository;
/**
 * 成绩分析     小题分析
 * 
 */
@CrossOrigin
@RestController
public class LeQuestionAnalysisController extends YxyApiBaseController{
	
	@Autowired
    private ExamQuesOverviewAnalyseRespository examQuesOverviewAnalyseRespository;
	@Autowired
	private ExamQuesAnalyseRepsository examQuesAnalyseRepsository;
	@RequestMapping(value = "/api_yw/manage/tec/teaching/scoreport/lequestionanalysis/srh", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public RespModel precess(HttpServletRequest request, HttpServletResponse response,@RequestBody ReqModel reqModel){
		Exam exam = new Exam();
		exam.setId(reqModel.examId);
		ExamQuesOverviewAnalyse examQuesOverviewAnalyse = examQuesOverviewAnalyseRespository.findByExam(exam);
		ExamQuesOverviewAnalyseModel examQuesOverviewAnalyseModel = new ExamQuesOverviewAnalyseModel();
		if (examQuesOverviewAnalyse !=null && examQuesOverviewAnalyse.getId()!=null && !examQuesOverviewAnalyse.equals("")) {			
			examQuesOverviewAnalyseModel.id = examQuesOverviewAnalyse.getId();
		}else{
			examQuesOverviewAnalyseModel.id = "";
		}
		if (examQuesOverviewAnalyse !=null && examQuesOverviewAnalyse.getQuesSum()!=null) {			
			examQuesOverviewAnalyseModel.quesSum = examQuesOverviewAnalyse.getQuesSum();
		}else{
			examQuesOverviewAnalyseModel.quesSum = 0;
		}
		if (examQuesOverviewAnalyse!=null && examQuesOverviewAnalyse.getLosePointRate()!=null) {			
			examQuesOverviewAnalyseModel.losePointRate = examQuesOverviewAnalyse.getLosePointRate();
		}else{
			examQuesOverviewAnalyseModel.losePointRate = 0.0;
		}
		if (examQuesOverviewAnalyse!=null && examQuesOverviewAnalyse.getMaxScoreQues()!=null) {	
			examQuesOverviewAnalyseModel.maxScoreQues = examQuesOverviewAnalyse.getMaxScoreQues().split(",");
		}else{
			examQuesOverviewAnalyseModel.maxScoreQues = null;
		}
		if (examQuesOverviewAnalyse!=null && examQuesOverviewAnalyse.getPaperLosePointRate()!=null) {			
			examQuesOverviewAnalyseModel.paperLosePointRate = examQuesOverviewAnalyse.getPaperLosePointRate();
		}else{
			examQuesOverviewAnalyseModel.paperLosePointRate = 0.0;
		}
		
		RespModel model = new RespModel();
		model.examQuesOverviewAnalyseModel = examQuesOverviewAnalyseModel;
		
		Pageable pageable = new PageRequest(reqModel.page, reqModel.size);
		
		Page<ExamQuesAnalyse> examQuesAnalyseLst = null; 
		examQuesAnalyseLst = examQuesAnalyseRepsository.findByExamOrderByOrderNumAsc(exam,pageable);
		
		ExamQuesAnalyseModel examQuesAnalyseModel =null;
		List<ExamQuesAnalyseModel> examQuesAnalyseModelLst = new ArrayList<ExamQuesAnalyseModel>();
		
		List<Double> doubles=new ArrayList<Double>();
		Page<ExamQuesAnalyseModel> examQuesAnalyseModelLst2 = new PageImpl<ExamQuesAnalyseModel>(examQuesAnalyseModelLst);
		if (examQuesAnalyseLst.getContent() == null || examQuesAnalyseLst.getContent().size() == 0) {
			model.isNull = false;
		}else{
			model.isNull = true;
			if (examQuesAnalyseLst !=null && examQuesAnalyseLst.getSize()>0) {
				
				for (ExamQuesAnalyse examQuesAnalyse : examQuesAnalyseLst) {
					examQuesAnalyseModel = new ExamQuesAnalyseModel();
					examQuesAnalyseModel.quesId = examQuesAnalyse.getId();
//					examQuesAnalyseModel.sectionName = examQuesAnalyse.getSectionName()==null?"":examQuesAnalyse.getSectionName();
					examQuesAnalyseModel.noName = (examQuesAnalyse.getSectionName()==null?"":examQuesAnalyse.getSectionName()) +" "+ (examQuesAnalyse.getNoName()==null?"":examQuesAnalyse.getNoName());
//					examQuesAnalyseModel.orderNum = examQuesAnalyse.getOrderNum();
//					examQuesAnalyseModel.section = examQuesAnalyse.getSection();
//					examQuesAnalyseModel.no = examQuesAnalyse.getNo();
					examQuesAnalyseModel.knowRemark = examQuesAnalyse.getKnowRemark() == null?"":examQuesAnalyse.getKnowRemark();
					examQuesAnalyseModel.remark = examQuesAnalyse.getRemark()==null?"":examQuesAnalyse.getRemark();
					examQuesAnalyseModel.difficultyLevel = examQuesAnalyse.getDifficultyLevel()==null?0.0:examQuesAnalyse.getDifficultyLevel();
					examQuesAnalyseModel.waveLevel = examQuesAnalyse.getWaveLevel()==null?0.0:examQuesAnalyse.getWaveLevel();
					examQuesAnalyseModel.rightRate = examQuesAnalyse.getRightRate()==null?0.0:examQuesAnalyse.getRightRate();
					if (examQuesAnalyseModel.rightRate < 0.4) {
						examQuesAnalyseModel.universality = "1";
					}else{
						examQuesAnalyseModel.universality = "0";
					}
					List<RecommendResource> recommendResourceLst=new ArrayList<RecommendResource>();
					if (examQuesAnalyse.getRecommend() != null) {
						RecommendResource recommendResource = null;
						for (Res res : examQuesAnalyse.getRecommend()) {
							recommendResource = new RecommendResource();
							recommendResource.id = res.id;
							recommendResource.extension = res.extension;
							recommendResource.title = res.title;
							recommendResource.location = res.location;
							recommendResourceLst.add(recommendResource);
						}
						examQuesAnalyseModel.resourceLst = recommendResourceLst;
					}else{
						examQuesAnalyseModel.resourceLst = recommendResourceLst;
					}
					
					examQuesAnalyseModel.accept = examQuesAnalyse.getAccept();
					examQuesAnalyseModelLst.add(examQuesAnalyseModel);
					doubles.add(examQuesAnalyseModel.difficultyLevel);
				}
			}
		
		
		Double hard=0.0;
		Double easy=0.0;
		if (doubles.size()>0) {
			//本班最难题标记值
			hard=Collections.max(doubles);//如果在前端便利的时候难度值等于本难度值就显示本班最难题
			//本班最简单标记值
			easy=Collections.min(doubles);//如果在前端便利的时候难度值等于本难度值就显示本班最简单
		}
		List<ExamQuesAnalyseModel> examQuesAnalyseModelLst3 = new ArrayList<ExamQuesAnalyseModel>();
		if (examQuesAnalyseModelLst !=null && examQuesAnalyseModelLst.size()>0) {
			for (ExamQuesAnalyseModel examQuesAnalyse : examQuesAnalyseModelLst) {
				if (examQuesAnalyse.difficultyLevel == easy || examQuesAnalyse.difficultyLevel.equals(easy)) {
					examQuesAnalyse.hardEasy = "本班最难题";
				}else if (examQuesAnalyse.difficultyLevel == hard || examQuesAnalyse.difficultyLevel.equals(hard)) {
					examQuesAnalyse.hardEasy = "本班最简单";
				}else{
					examQuesAnalyse.hardEasy = "";
				}
				examQuesAnalyseModelLst3.add(examQuesAnalyse);
			}
		}
		examQuesAnalyseModelLst2 = new PageImpl<ExamQuesAnalyseModel>(examQuesAnalyseModelLst3, pageable, examQuesAnalyseLst.getTotalElements());
		}
		model.examQuesAnalyseModelLst = examQuesAnalyseModelLst2;
        return model;
    }

    protected static class RespModel extends YxyApiRespModel {
    	public ExamQuesOverviewAnalyseModel examQuesOverviewAnalyseModel;
    	public Page<ExamQuesAnalyseModel> examQuesAnalyseModelLst;
    	public Boolean isNull;
    }
    protected static class ReqModel extends YxyApiPageReqModel {
    	public String examId;
    }
    protected class ExamQuesOverviewAnalyseModel{
    	public String id;
    	//试题数量
    	public Integer quesSum;
    	//题均失分率
    	public Double losePointRate;
    	//得分率最高的题号
    	public String[] maxScoreQues;
    	//本次卷面失分率
    	public Double paperLosePointRate;
    }
    protected class ExamQuesAnalyseModel{
    	//试题id
    	public String quesId;
    	// 大题号
//        public Integer section;
        //大题号名称
//        public String sectionName;
        // 小题号
//        public Integer no;
        // 小题号名称
        public String noName;
    	//序列号
//    	public Integer orderNum;
        // 详细分析
    	public String remark;
    	// 难度值
    	public Double difficultyLevel;
    	// 波动度
    	public Double waveLevel;
    	// 正确率
    	public Double rightRate;
    	// 普遍性失分值  1:普遍性失分0:空字符串
    	public String universality;
    	//判断是否是最难题和最简单题
    	public String hardEasy;
    	//知识点分析
    	public String knowRemark;
    	// 推荐的资源
        public List<RecommendResource> resourceLst;
        //采纳 0:不采纳    1:采纳
        public Integer accept; 
    } 
    protected class RecommendResource {
        // 文件扩展名
        public String extension;
        // 标题
        public String title;
        // 文件id
        public String id;
        //url
        public String location;
    }
}
